/*
filename out1972 'H:\Private\Poverty Trends\SAS\cps\output\taxsim1972.txt';
filename out1973 'H:\Private\Poverty Trends\SAS\cps\output\taxsim1973.txt';
filename out1974 'H:\Private\Poverty Trends\SAS\cps\output\taxsim1974.txt';
filename out1975 'H:\Private\Poverty Trends\SAS\cps\output\taxsim1975.txt';
*/

%macro read(surv_yr);
proc sort data=cpsdata.all&surv_yr;
by famid;
run;

data taxsim&surv_yr;
set cpsdata.all&surv_yr;
by famid;

* Create var for person number to be used in id var;
RETAIN counter 0; 
counter= counter+1;
IF FIRST.famid THEN DO; 
counter=1;
END;

/* This is the value for the gross income test for dependents.  It is the same
   as the personal exemption.  
   See data h:data\tax\tax_parameters_taxpolicycenter.org.xls
   The parameters are for tax years 1971-2005--CPS years 1972-2004
*/
	IF &surv_yr=1972 THEN inc_test=675;  
	ELSE IF &surv_yr IN(1973,1974,1975,1976,1977,1978,1979) THEN inc_test=750; 

* Creat dependents var, including both children and non-working unmarried relatives;
IF 		(famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31)
		/* don't work at all or work little because you go to school */
		AND (rsnnotw NE 0 OR (wkswrk IN (0,1,2) AND rsnnotl=4)) 
		AND marstat IN(0,1,6,7) 
		AND ptotinc LT inc_test 
		THEN dep=1;

* allow married non-working children 19-23 to be dependents;
ELSE IF (famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31) 
			AND 19 LE age LE 23 
			/* don't work at all or work little because you go to school */
			AND (rsnnotw NE 0 OR (wkswrk IN (0,1,2) AND rsnnotl=4)) THEN dep=1;

* allow all children under 19 to be dependents;
ELSE IF (famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31) 
			AND age LE 18 THEN dep=1;
ELSE dep=0;

* calc num of kids for fed child tax credit;
IF (famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31) 
			AND age LE 16 THEN kids3=1;
ELSE kids3=0;

* calc num of eitc kids;
IF (famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31) 
			AND age LE 18 THEN kids2=1;
ELSE IF (famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31) 
			AND 19 LE age LE 23 
			AND (rsnnotw IN(1,3) OR (wkswrk IN (0,1,2) AND rsnnotl IN(2,4))) THEN kids2=1;
ELSE kids2=0;

* calc num of taxpayers over 65, exclude dependents over 65;
IF age GT 65 AND dep=0 THEN gt65=1;
ELSE gt65=0;
/* **********************   JH's add   ************************ */
IF (famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31) AND age LE 17 THEN dep18=1; ELSE dep18=0;
IF (famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31) AND age LE 16 THEN dep17=1; ELSE dep17=0;
IF (famrel2 IN(4,7,9,12,13,15,16,19,21,24) OR 27 LE famrel2 LE 31) AND age LE 12 THEN dep13=1; ELSE dep13=0;
IF famrel2 IN(2,6,11,18,23,26) THEN spage=age; ELSE spage=0;
/* ************************************************************ */ 
IF famrel2 IN(2,6,11,18,23,26) THEN wife_inc=ptotearn;
ELSE wife_inc=0;

run;

proc sort data=taxsim&surv_yr;
by famid;
run;

proc means data=taxsim&surv_yr sum noprint;
by famid;
* restrict to husbands, wives, and their dependents;
/* ****************  Variable added by JH   ******************** */
where famrel2 IN(1,2,3,5,6,8,10,11,14,17,18,20,22,23,25,26) OR dep=1;
var dep kids2 kids3 wife_inc gt65 ssinc intinc divinc uiinc retinc alimony ptotearn dep18 dep17 dep13 spage;
output out=temp1 sum=totdep kid_eitc kid_fcc tfu_wifi tfu_gt65 tfu_ss tfu_int 
					 tfu_div tfu_ui tfu_ret tfu_alm tfu_earn totdep18 totdep17 totdep13 sage;
run;

data cpsdata.taxsim&surv_yr;
merge taxsim&surv_yr(IN=in1) temp1(KEEP=famid totdep kid_eitc kid_fcc tfu_wifi tfu_gt65 tfu_ss tfu_int 
					            tfu_div tfu_ui tfu_ret tfu_alm tfu_earn totdep18 totdep17 totdep13 sage);
by famid;
if in1;

* Restrict sample to heads of tax filing unit;
* Delete spouses;
IF famrel2 IN(2,6,11,18,23,26) THEN DELETE;
* Delete dependents;
/* ****************  Variable added by JH   ******************** */
IF dep=0;

     ARRAY tax_vars totdep kid_eitc kid_fcc tfu_wifi tfu_gt65 tfu_ss tfu_int 
					            tfu_div tfu_ui tfu_ret tfu_alm tfu_earn totdep18 totdep17 totdep13 sage;
	 DO OVER tax_vars;
	 IF tax_vars=. THEN tax_vars=0;
	 END; 

*adjustments for taxsim;
/* ****************  Variable added by JH   ******************** */
tfu_wifi=MAX(tfu_wifi,0);
totdep=MIN(totdep,15);
kid_eitc=MIN(kid_eitc,15);
kid_fcc=MIN(kid_fcc,15);
totdep18=MIN(totdep18,15);
totdep17=MIN(totdep17,15);
totdep13=MIN(totdep13,15);


/* NEW CODE: RECODE THE INDEPENDENT CHILDREN (WHO ARE THEIR OWN TAX FILING UNIT) TO HAVE NO SPOUSAL INCOME, DEPENDENTS, ETC */
/* ****************  Variable added by JH   ******************** */
IF (famrel2 IN(2,4,6,7,9,11,12,13,15,16,18,19,21,23,24) OR 26 LE famrel2 LE 34) THEN tfu_wifi=0;
IF (famrel2 IN(2,4,6,7,9,11,12,13,15,16,18,19,21,23,24) OR 26 LE famrel2 LE 34) THEN totdep=0;
IF (famrel2 IN(2,4,6,7,9,11,12,13,15,16,18,19,21,23,24) OR 26 LE famrel2 LE 34) THEN kid_fcc=0;
IF (famrel2 IN(2,4,6,7,9,11,12,13,15,16,18,19,21,23,24) OR 26 LE famrel2 LE 34) THEN kid_eitc=0;
IF (famrel2 IN(2,4,6,7,9,11,12,13,15,16,18,19,21,23,24) OR 26 LE famrel2 LE 34) THEN totdep18=0;
IF (famrel2 IN(2,4,6,7,9,11,12,13,15,16,18,19,21,23,24) OR 26 LE famrel2 LE 34) THEN totdep17=0;
IF (famrel2 IN(2,4,6,7,9,11,12,13,15,16,18,19,21,23,24) OR 26 LE famrel2 LE 34) THEN totdep13=0;
IF (famrel2 IN(2,4,6,7,9,11,12,13,15,16,18,19,21,23,24) OR 26 LE famrel2 LE 34) THEN sage=0;


IF famrel2 IN(1,3,5,8,10,14,17,20,22,25) THEN taxinc=tfu_earn-tfu_wifi;
ELSE taxinc=ptotearn;
taxinc=MAX(taxinc,0);

IF famrel2 IN(1,3,5,8,10,14,17,20,22,25) THEN inc9=tfu_div;
ELSE inc9=divinc;
inc9=MAX(inc9,0);

IF famrel2 IN(1,3,5,8,10,14,17,20,22,25) THEN inc10=tfu_int;
ELSE inc10=intinc;

IF famrel2 IN(1,3,5,8,10,14,17,20,22,25) THEN inc11=tfu_ret;
ELSE inc11=retinc;

IF famrel2 IN(1,3,5,8,10,14,17,20,22,25) THEN inc12=tfu_ss;
ELSE inc12=ssinc;

IF famrel2 IN(1,3,5,8,10,14,17,20,22,25) THEN inc18=tfu_ui;
ELSE inc18=uiinc;

IF famrel2 IN(1,3,5,8,10,14,17,20,22,25) THEN num_gt65=tfu_gt65;
ELSE num_gt65=gt65;

id=famid*100+counter;

/* ****************  codes are edited by JH   ******************** */
IF marstat IN(2,4,5) THEN filestat=2;
   ELSE filestat=1;

***********************************************************
Recode from CPS state id to the SOI state id used by taxsim
dont know what state2=25 means so make them ny
***********************************************************;
IF &surv_yr=1972 THEN DO;
IF state2=11 THEN state=7;
ELSE IF state2=19 THEN state=22;
ELSE IF state2=21 THEN state=33;
ELSE IF state2=22 THEN state=31;
ELSE IF state2=23 THEN state=39;
ELSE IF state2=31 THEN state=36;
ELSE IF state2=32 THEN state=15;
ELSE IF state2=33 THEN state=14;
ELSE IF state2=39 THEN state=23;
ELSE IF state2=41 THEN state=24;
ELSE IF state2=43 THEN state=26;
ELSE IF state2=49 THEN state=17;
ELSE IF state2=51 THEN state=9;
ELSE IF state2=52 THEN state=21;
ELSE IF state2=53 THEN state=49;
ELSE IF state2=54 THEN state=11;
ELSE IF state2=55 THEN state=10;
ELSE IF state2=57 THEN state=34;
ELSE IF state2=59 THEN state=47;
ELSE IF state2=61 THEN state=18;
ELSE IF state2=62 THEN state=43;
ELSE IF state2=69 THEN state=1;
ELSE IF state2=71 THEN state=19;
ELSE IF state2=72 THEN state=44;
ELSE IF state2=79 THEN state=37;
ELSE IF state2=81 THEN state=6;
ELSE IF state2=89 THEN state=45;
ELSE IF state2=91 THEN state=38;
ELSE IF state2=92 THEN state=5;
ELSE IF state2=99 THEN state=48;
ELSE IF state2=25 THEN state=33;
END;

IF 1973 LE &surv_yr LE 1975 THEN DO;
IF state2=16 THEN state=7;
ELSE IF state2=14 THEN state=22;
ELSE IF state2=19 THEN state=20;
ELSE IF state2=21 THEN state=33;
ELSE IF state2=22 THEN state=31;
ELSE IF state2=23 THEN state=39;
ELSE IF state2=31 THEN state=36;
ELSE IF state2=32 THEN state=15;
ELSE IF state2=33 THEN state=14;
ELSE IF state2=39 THEN state=23;
ELSE IF state2=43 THEN state=26;
ELSE IF state2=49 THEN state=24;
ELSE IF state2=53 THEN state=9;
ELSE IF state2=56 THEN state=34;
ELSE IF state2=57 THEN state=21;
ELSE IF state2=58 THEN state=11;
ELSE IF state2=59 THEN state=10;
ELSE IF state2=67 THEN state=43;
ELSE IF state2=69 THEN state=1;
ELSE IF state2=72 THEN state=44;
ELSE IF state2=79 THEN state=19;
ELSE IF state2=81 THEN state=3;
ELSE IF state2=89 THEN state=6;
ELSE IF state2=92 THEN state=5;
ELSE IF state2=99 THEN state=38;

END;

fill=0;

**********************   variables added by JH   **************************;
KEEP id ref_year state filestat totdep num_gt65 taxinc tfu_wifi
   inc9 inc10 inc11 inc12 fill inc18 kid_fcc kid_eitc totdep18 totdep17 totdep13 age sage;
RUN;

PROC MEANS;
RUN;
/*
DATA _NULL_;
   SET cpsdata.taxsim&surv_yr;
   FILE out&surv_yr lrecl=500;
   PUT id ref_year state filestat totdep num_gt65 taxinc tfu_wifi
   inc9 inc10 inc11 inc12 fill fill fill fill fill inc18 kid_fcc fill fill fill;
RUN;
*/
%mend read;

/* %read(1972); */
%read(1973);
%read(1974);
%read(1975);

****************************************************************
 FTP MAINDAT TO TAXSIM (http://users.nber.org/~taxsim/taxsim27/)
 NOTE SURE IF THIS WILL WORK 
****************************************************************;

%let DATAFILE = %SUBSTR(&SYSTIME,1,2)%SUBSTR(&SYSTIME,4,2);
%PUT 'Filename on taxsimftp server is:' &DATAFILE;

filename txpydata ftp "&DATAFILE"         host='taxsimftp.nber.org'
         user='taxsim' pass='02138' cd='tmp' passive ;
filename results  ftp "&DATAFILE..txm27" host='taxsimftp.nber.org'
         user='taxsim' pass='02138' cd='tmp' passive lrecl=1024;
filename errors   ftp "&DATAFILE..msg" host='taxsimftp.nber.org'
         user='taxsim' pass='02138' cd='tmp' passive ;


*****              Variables that need to be submitted to TAXSIM27                   *******;
*****  State tax info goes back to 1977. Change state to fill for years before 1977  *******;
%macro create_dat(yr);

data;
set cpsdata.taxsim&yr;
file txpydata;
put id ref_year fill filestat age sage totdep totdep13 totdep17 totdep18 taxinc tfu_wifi 
    inc9 fill fill fill inc10 fill inc11 inc12 inc18 fill fill fill fill fill fill;
run;

* old version *;
/*
put id ref_year state filestat totdep num_gt65 taxinc tfu_wifi
   inc9 inc10 inc11 inc12 fill fill fill fill fill inc18 kid_fcc fill fill fill;


/* Read results directly from taxsimftp server. */

DATA ts_all;
	INFILE results firstobs=2;
	INPUT id year ts_state ftl stl ts_fica;
RUN;

PROC MEANS DATA=ts_all;
RUN;

DATA taxsim.taxs&yr;
   SET ts_all;
RUN;
PROC SORT data=taxsim.taxs&yr;
BY id;
RUN;

/* Read and print to log the taxsim error message file. */
data;
infile errors;
input;put 'TAXSIM:' _infile_;
run;

%mend create_dat;
/* %create_dat(1972); */
%create_dat(1973);
%create_dat(1974);
%create_dat(1975);
